package org.agilelang.editor.tool;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "InfoProject")
public class InfoProject {

    public String name;
    public String version;

    public void save(String path) {
        JAXBContext jc = null;
        Marshaller marshaller;
        java.io.StringWriter sw = new StringWriter();
        try {
            jc = JAXBContext.newInstance(org.agilelang.editor.tool.InfoProject.class);
            marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(this, sw);

        } catch (JAXBException ex) {
            System.err.println("Error --> " + ex.toString());
        }

        try {
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path + File.separator + "project.pa")));
            pw.print(sw.toString());
            pw.close();
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    public void open(String path) {
        File file = new File(path);
        if (!file.exists()) {
            System.err.println("Fichero XMl no existe");
            return;
        }        
        InputStream is = null;
        Reader reader = null;
        try {
            is = new FileInputStream(path);
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            System.out.println("Reding xml...");
        } catch (Exception ex) {
            System.err.println("Error al leer fichero XML : " + ex);
        }          
        JAXBContext jc;
        try {
            jc = JAXBContext.newInstance(InfoProject.class);
            Unmarshaller u = jc.createUnmarshaller();
            org.agilelang.editor.tool.InfoProject info = (org.agilelang.editor.tool.InfoProject) u.unmarshal(reader);
            this.name = info.name;
            this.version = info.version;
            System.out.println("Built project...");
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto  : " + ex.toString());
        }
    }
}
